package com.share.domain.activity.service.partake;

import com.alibaba.fastjson.JSON;
import com.share.domain.activity.model.aggregate.CreatePartakeOrderAggregate;
import com.share.domain.activity.model.entity.ActivityEntity;
import com.share.domain.activity.model.entity.PartakeRaffleActivityEntity;
import com.share.domain.activity.model.entity.UserRaffleOrderEntity;
import com.share.domain.activity.model.valobj.ActivityStateVo;
import com.share.domain.activity.repository.IActivityRepository;
import com.share.domain.activity.service.quota.IRaffleActivityPartakeService;
import com.share.types.enums.ResponseCode;
import com.share.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * 抽奖活动参与抽象类
 */

@Slf4j
public abstract class AbstractRaffleActivityPartake implements IRaffleActivityPartakeService {

    protected final IActivityRepository activityRepository;

    public AbstractRaffleActivityPartake(IActivityRepository activityRepository) {
        this.activityRepository = activityRepository;
    }

    @Override
    public UserRaffleOrderEntity createOrder(PartakeRaffleActivityEntity partakeRaffleActivityEntity) {
        // 0.基础信息
        Long activityId = partakeRaffleActivityEntity.getActivityId();
        String userId = partakeRaffleActivityEntity.getUserId();
        Date currentDate = new Date();

        //1.活动查询
        ActivityEntity activityEntity = activityRepository.queryRaffleActivityByActivityId(activityId);
        if(!ActivityStateVo.open.getCode().equals(activityEntity.getState().getCode())) {
            //活动未开启
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(), ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }
        if(activityEntity.getBeginDateTime().after(currentDate)||activityEntity.getEndDateTime().before(currentDate)){
            //未在活动时间内
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(),ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }

        //2.查询未被使用的活动订单记录
        UserRaffleOrderEntity userRaffleOrderEntity = activityRepository.queryNoUsedRaffleOrder(partakeRaffleActivityEntity);
        if(null!=userRaffleOrderEntity) {
            log.info("已存在未被使用的活动订单记录,使用该记录orderId:{},activityId:{},userRaffleOrderEntity:{}",userRaffleOrderEntity.getOrderId(),userRaffleOrderEntity.getActivityId(), JSON.toJSONString(partakeRaffleActivityEntity));
            return userRaffleOrderEntity;
        }

        //3.账户额度过滤:返回账户的构建对象
        CreatePartakeOrderAggregate createPartakeOrderAggregate = this.doFilterAccount(userId,activityId,currentDate);

        //4.创建订单
        UserRaffleOrderEntity userRaffleOrder =this.buildUserRaffleOrder(userId,activityId,currentDate);
        //订单填充到聚合对象中
        createPartakeOrderAggregate.setUserRaffleOrderEntity(userRaffleOrder);

        //保存聚合对象,一个领域内的聚合是一个事务操作
        activityRepository.saveCreatePartakeOrderAggregate(createPartakeOrderAggregate);

        return userRaffleOrder;
    }

    @Override
    public UserRaffleOrderEntity createOrder(String userId, Long activityId) {
        return createOrder(PartakeRaffleActivityEntity.builder()
                .userId(userId)
                .activityId(activityId)
                .build());
    }

    protected abstract UserRaffleOrderEntity buildUserRaffleOrder(String userId, Long activityId, Date currentDate);

    protected abstract CreatePartakeOrderAggregate doFilterAccount(String userId, Long activityEntity, Date currentDate);
}
